// pages/integralData/integralData.js
var Ajax = require('../../utils/Ajax.js')
var utils = require('../../utils/util.js');
var config = require('../../base.conf.js').config;
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    moreType: 'noMore',
    statusType: 'none', //数据查询错误并且数组为空 true为错误 false正常
    statuserro: false, //数据是否为空 value:empty
    getUserStep: '',
    moreType: 'none',
    currentPage: 1,
    pageSize: 20,
    IntegralInfo:"",
    recordArr: []
  },
  onSetid(e) { //弹出窗的ID获取
    var id = 'modal'
    if (typeof(e) == 'string') {
      id = e
    } else {
      id = e && (e.currentTarget.id || e.currentTarget.dataset.id);
    }
    // console.log(id)
    return id;
  },
  getIntegralInfo() {//获取积分规则
    Ajax.get('getIntegralInfo').then((d) => {
      if (d.code == 0) {
        this.setData({
          IntegralInfo: d.data
        })
      }
    }).catch((e) => {
      console.log(e)
    })
  },
  showModal(e) { //显示或隐藏弹窗
    var id = this.onSetid(e);
    var modal = this.selectComponent("#" + (id || 'modal'));
    if (modal.isShow()) {
      modal.hide()
    } else {
      modal.show();
    }
  },
  closeModal(e) { //关闭弹窗
    var id = this.onSetid(e);
    var modal = this.selectComponent("#" + (id || 'modal'));
    modal.hide();
  },
  lookrules(e) {
    console.log()
    this.showModal('RulesA')
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

    utils.showLoading();
    const sysInfo = wx.getSystemInfoSync();
    const screenWidth = sysInfo.screenWidth;
    this.windowHeight = sysInfo.windowHeight;
    this.factor = screenWidth / 750;
    this.setData({
      nogoodsH: (this.windowHeight / this.factor) - 112-130,
      getUserStep: utils.getUserStep() || null,
    }, () => {
      this.refresh();
    })
  },
  refresh() { //下拉刷新
    var {
      moreType,
      currentPage,
    } = this.data;
    if (moreType != "loading") {
      this.setData({
        currentPage: 1,
        moreType: 'none'
      }, () => {
        this.getIntegralList();
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },
  getIntegralList() {
    var {
      getUserStep,
      currentPage,
      pageSize,
      moreType,
      nogoodsH
    } = this.data;
    Ajax.get('getIntegralList', {
      uuid: getUserStep && getUserStep.uuid,
      currentPage,
      pageSize
    }).then((d) => {
      wx.stopPullDownRefresh();
      utils.hideLoading();
      moreType = "showText";
      if (d.status == 200 && d.data) {
        var list = d.data.pageResult.list || [];
        list.length < pageSize ? moreType = "noMore" : moreType
        if (currentPage != 1) {
          list = this.data.recordArr.concat(list);
        }
        var listHeight = list.length * 124+100;
        if (listHeight > nogoodsH) {
          listHeight = nogoodsH
        }
        this.setData({
          recordArr: list,
          scoorh: listHeight,
          statusType: list.length > 0 ? 'none' : 'empty',
          statuserro: false,
          moreType
        })
      } else {
        var {
          recordArr
        } = this.data;
        this.setData({
          statuserro: recordArr.length > 0 ? false : true,
          statusType: recordArr.length > 0 ? 'none' : 'empty',
        })
        utils.showtoast(d.msg || '服务器错误,查询失败')
      }
    }).catch((e) => {
      console.log(e)
      utils.hideLoading();
      wx.stopPullDownRefresh();
      var {
        recordArr
      } = this.data;
      this.setData({
        statuserro: recordArr.length > 0 ? false : true,
        statusType: recordArr.length > 0 ? 'none' : 'empty',
      })
      utils.showtoast('网络错误查询失败')
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.getIntegralInfo();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    this.refresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  scrolltolower: function() { //滚动触底加载
    var {
      moreType,
      currentPage,
    } = this.data;
    if (moreType == "showText") {
      var currentPage = currentPage + 1;
      this.setData({
        currentPage,
        moreType: "loading"
      }, () => {
        this.getIntegralList();
      });
    }
  },
})